﻿using System.Collections.Generic;

namespace ClockwiseClient
{
    public enum EnumSorter
    {
        SORTER_INVALID = 0,
        SORTER_CODE,
        SORTER_NAME,
        SORTER_PRICE,
        SORTER_DIFF,
        SORTER_CHANGE,
        SORTER_BUY,
        SORTER_SELL,
        SORTER_PRECLOSE,
        SORTER_OPEN,
        SORTER_HIGH,
        SORTER_LOW,
        SORTER_AMPLITUDE,
        SORTER_UPLIMIT,
        SORTER_DOWNLIMIT,
        SORTER_SHOT,
        SORTER_VOLUME,
        SORTER_AMOUNT,

        SORTER_COUNT
    }

    public class ClassSorter
    {
        protected List<ClassSorterItem> lstSorter;
        protected bool sortByDesc;  // 是否倒序？

        public virtual EnumSorter GetSorterType() { return EnumSorter.SORTER_INVALID; }

        public virtual int GetIndex(int position)
        {
            if (position < 0 || position >= lstSorter.Count) return -1;
            ClassSorterItem item;
            item = lstSorter[position];
            return item.GetPosition();
        }

        public ClassSorter(bool sortDir)
        {
            sortByDesc = sortDir;
        }

        public virtual void Builder(List<ClassFilterItem> lstFilter) {}

        public virtual void sort()
        {
            for (int i = 0; i < lstSorter.Count - 1; i++)
            {
                for (int j = i + 1; j < lstSorter.Count; j++)
                {
                    ClassSorterItem item1 = lstSorter[i];
                    ClassSorterItem item2 = lstSorter[j];
                    int eq = item1.GetValue().CompareTo(item2.GetValue());
                    if (!sortByDesc && eq > 0 || sortByDesc && eq < 0)
                    {
                        int k = item1.GetPosition();
                        item1.SetPosition(item2.GetPosition());
                        item2.SetPosition(k);
                        string s = item1.GetValue();
                        item1.SetValue(item2.GetValue());
                        item2.SetValue(s);
                    }
                }
            }

        }

        public virtual void sortF()
        {
            for (int i = 0; i < lstSorter.Count - 1; i++)
            {
                for (int j = i + 1; j < lstSorter.Count; j++)
                {
                    ClassSorterItem item1 = lstSorter[i];
                    ClassSorterItem item2 = lstSorter[j];
                    if (!sortByDesc && item1.GetValueF() > item2.GetValueF() || sortByDesc && item1.GetValueF() < item2.GetValueF())
                    {
                        int k = item1.GetPosition();
                        item1.SetPosition(item2.GetPosition());
                        item2.SetPosition(k);
                        float f = item1.GetValueF();
                        item1.SetValueF(item2.GetValueF());
                        item2.SetValueF(f);
                    }
                }
            }

        }
    }

    public class ClassSorterItem
    {
        int position;
        float f_value;
        string value;

        public ClassSorterItem(int pos, float f_va)
        {
            position = pos;
            f_value = f_va;
        }

        public ClassSorterItem(int pos, string va)
        {
            position = pos;
            value = va;
        }

        public int GetPosition()
        {
            return position;
        }

        public float GetValueF()
        {
            return f_value;
        }

        public string GetValue()
        {
            return value;
        }

        public void SetPosition(int pos)
        {
            position = pos;
        }


        public void SetValueF(float f_va)
        {
            f_value = f_va;
        }

        public void SetValue(string va)
        {
            value = va;
        }
    }

    public class ClassSorterCode : ClassSorter
    {
        public ClassSorterCode(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_CODE; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetCode(item.position)));
            }
        }
    }

    public class ClassSorterName : ClassSorter
    {
        public ClassSorterName(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_NAME; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetName(item.position)));
            }
        }
    }

    public class ClassSorterPrice : ClassSorter
    {
        public ClassSorterPrice(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_PRICE; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetPrice(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterDiff : ClassSorter
    {
        public ClassSorterDiff(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_DIFF; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetDiff(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterChange : ClassSorter
    {
        public ClassSorterChange(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_CHANGE; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetChange(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterBuy : ClassSorter
    {
        public ClassSorterBuy(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_BUY; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetBuy(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterSell : ClassSorter
    {
        public ClassSorterSell(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_SELL; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetSell(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterShot : ClassSorter
    {
        public ClassSorterShot(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_SHOT; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetDelta(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterVolume : ClassSorter
    {
        public ClassSorterVolume(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_VOLUME; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetVolume(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterAmount : ClassSorter
    {
        public ClassSorterAmount(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_AMOUNT; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetAmount(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterPreclose : ClassSorter
    {
        public ClassSorterPreclose(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_PRECLOSE; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetPreclose(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterOpen : ClassSorter
    {
        public ClassSorterOpen(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_OPEN; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetOpen(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterHigh : ClassSorter
    {
        public ClassSorterHigh(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_HIGH; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetHigh(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterLow : ClassSorter
    {
        public ClassSorterLow(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_LOW; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetLow(item.position)));
            }
            sortF();
        }
    }

    public class ClassSorterAmplitude : ClassSorter
    {
        public ClassSorterAmplitude(bool sortDir) : base(sortDir) { }
        public override EnumSorter GetSorterType() { return EnumSorter.SORTER_AMPLITUDE; }
        public override void Builder(List<ClassFilterItem> lstFilter)
        {
            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();
            lstSorter = new List<ClassSorterItem>();
            foreach (ClassFilterItem item in lstFilter)
            {
                lstSorter.Add(new ClassSorterItem(item.position, classStockData.GetAmplitude(item.position)));
            }
            sortF();
        }
    }
}
